<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Response;
use app\models\LoginForm;
use app\models\ContactForm;

class UsersController extends AuthController
{
    public $currentHeader = 'header-operation';
    public $currentNav = 'users-nav';
    public $pageTitle = '';

    public function actionUserList() {
        $this->pageTitle = $this->t('user', 'User List');
        return $this->show();
    }

    public function actionNewUser() {
        $this->pageTitle = $this->t('user', 'New User');
        $this->advertisers = $this->service->getAll('Advertiser');
        $this->subUsers = $this->service->getAll('User', 'user_type in (3,4)');
        $this->adChannels = $this->service->getAllAdNetWork();
        $this->trafficTypes = $this->service->getTrafficTypes();
        $this->medias = $this->service->getAdspaceMedias();
        return $this->show();
    }

    public function actionUserEdit() {
        $this->pageTitle = $this->t('user', 'Edit User');
        $userId = $this->I('id');
        $user = $this->M('User', $userId);
        $this->advertisers = $this->service->getAll('Advertiser');
        $domainAdvertiser = $this->service->getUserAdvertisers($user);
        $this->domainAdvertiser = array_keys($domainAdvertiser);
        $subUsers = $this->service->getAll('User', 'user_type in (3,4)');
        $this->subUsers = $subUsers;
        $domainUser = $this->service->getUserSubordinateIds($user, 1);
        $this->domainUser = $domainUser;
        unset($user['password']);
        $this->user = $user;
        $this->aUser = $user;
        $this->adChannels = $this->service->getAllAdNetWork();
        $this->trafficTypes = $this->service->getTrafficTypes();
        $this->medias = $this->service->getAdspaceMedias();
        return $this->show();
    }

    public function actionNewUserHandle() {
        $this->service->saveNewUser($this->I());
        $this->OK($this->t('user', 'Add user successfully!'));
    }

    public function actionUserEditHandle() {
        $this->service->saveNewUser($this->I(), false);
        $this->OK($this->t('user', 'Edit user successfully!'));
    }

    public function actionGetUsers() {
        $users = $this->service->getAllUsers();
        $this->jsonReturn(['data' => $users]);
    }

    public function actionListOperateHandle () {
        $type = $this->I('type');
        if ($type == 'reset') {
            $this->service->resetPassword($this->I('user_id'));
            $this->OK($this->t('user', 'Reset password successfully!'));
        }
        $this->NG($this->t('user', 'Reset password failed!'));
    }
}// end of users controller declaration
